Exercise: Sorting 1. ˆ Let A be any array. ˆ Write a program which outputs the sorted array of A (in ascending order).

Size: px
Start display at page:

Download "Exercise: Sorting 1. ˆ Let A be any array. ˆ Write a program which outputs the sorted array of A (in ascending order)."

Transcription

1 Exercise: Sorting 1 ˆ Let A be any array. ˆ Write a program which outputs the sorted array of A (in ascending order). ˆ For example, A = [5, 4, 1, 2, 3]. ˆ Then the sorted array is [1, 2, 3, 4, 5]. ˆ You may use sort. 1 See Zheng-Liang Lu 134 / 179

2 Practical Exercise 1 >> stocks = {'Google', 15;... 2 'Facebook', 12;... 3 'Apple', 18}; 4 >> [~, index] = sort([stocks{:, 2}], 'descend') 5 6 index = >> stocks = stocks(index, :) stocks = 'Apple' [18] 15 'Google' [15] 16 'Facebook' [12] Zheng-Liang Lu 135 / 179

3 Another Exercise: Shuffling ˆ Write a program which randomly permutes the elements of the input array A. ˆ For example, A = [1, 2, 3, 4, 5]. ˆ Then a possible permutation of A is [4, 2, 1, 3, 5]. ˆ Make sure your program correct in statistical sense. 2 ˆ Verify by Monte Carlo simulation. ˆ Write another program which enumerates all possible permutations of A. 3 2 Read 3 See salgorithm. Zheng-Liang Lu 136 / 179

4 1 >> A = 'abcdef'; 2 >> A = A(randperm(length(A))) 3 4 A = 5 6 dafbce ˆ You may use randperm(a) to generate an index array to permute the elements of A randomly. Zheng-Liang Lu 137 / 179

5 Analysis of Algorithms ˆ Given a problem, suppose that there exist various algorithms. ˆ Then we investigate these algorithms in various dimensions and choose the most appropriate one. ˆ Normally we want efficient algorithms. ˆ We now define the growth rate of the running time as a function of input size n, denoted by f (n). ˆ For simplicity, assume that every instruction takes a unit time. ˆ Can you find f (n) for each program? Zheng-Liang Lu 138 / 179

6 O-notation 4 ˆ In math, O-notation describes the limiting behavior of a function, usually in terms of simple functions. ˆ We say that f (n) O(g(n)) as n if and only if there is a constant c > 0 and a real number n 0 such that f (n) c g(n) n n 0. (1) ˆ So O(g(n)) is a collection featured by some simple function g(n). ˆ f (n) O(g(n)) means that f (n) is one instance of O(g(n)). 4 See Zheng-Liang Lu 139 / 179

7 ˆ For example, 8n 2 3n + 4 O(n 2 ). ˆ Moreover, 8n 2 3n + 4 O(n 3 ) but 8n 2 3n + 4 / O(n). Zheng-Liang Lu 140 / 179

8 Fundamental Functions for Growth Rate 5 5 See Table 4.1 and Figure 4.2 in Goodrich and etc, p Zheng-Liang Lu 141 / 179

9 ˆ We use O-notation to describe the asymptotic 6 upper bound of complexity of the algorithm. ˆ So O-notation is widely used to classify algorithms by how they respond to changes in its input size. 7 ˆ Time complexity ˆ Space complexity ˆ Note that we often make a trade-off between time and space. ˆ Unlike time, we can reuse memory. 6 The asymptotic sense is that the input size n grows toward infinity. 7 Actually, there are Θ, θ, o, Ω, and ω which are used to classify algorithms. Zheng-Liang Lu 142 / 179

10 Faster Is Better? ˆ Consider a 10-year deposit account. ˆ You deposit 10, 000 TWD in the beginning of each year. ˆ Assume that the bank pays an annual interest rate r = 10% compounded annually. ˆ Determine the balance at the end of 10 years. ˆ The answer is 175, TWD. Zheng-Liang Lu 143 / 179

11 Zheng-Liang Lu 144 / 179

12 Solution 1 1 clear; clc; 2 3 r = 0.1; 4 years = 10; 5 6 s = 1; 7 for i = 1 : years s = s * (1 + r) + 1; 9 end 10 s = s * (1 + r) * 1e4 ˆ Time complexity: O(n) ˆ Space complexity: O(1) ˆ It is similar to Horner s method. Zheng-Liang Lu 145 / 179

13 Solution 2 1 clear; clc; 2 3 r = 0.1; 4 years = 10; 5 6 s = 0; 7 for i = 1 : years 8 s = s + (1 + r) ˆ i; 9 end 10 s = s * 1e4 ˆ Time complexity: O(n) ˆ Space complexity: O(1) Zheng-Liang Lu 146 / 179

14 Solution 3 1 clear; clc; 2 3 r = 0.1; 4 years = 10; 5 s = (1 + r) * ((1 + r) ˆ years - 1) / r * 1e4 ˆ Time complexity: O(1) (Why?) ˆ Space complexity: O(1) ˆ Pros: fastest and compact; Cons: not robust Zheng-Liang Lu 147 / 179

15 Real Case: Floating Interest Rate ˆ Instead of constant interest rates, assume that the sequence of floating interest rates is r = 0.1 : 0.01 : ˆ Determine the compound amount. ˆ The balance is 125, TWD. Zheng-Liang Lu 148 / 179

16 Solution 1 clear; clc; 2 3 r = 0.1 : : 0.01; 4 years = 10; 5 6 s = 1; 7 for i = 1 : years s = s * (1 + r(i)) + 1; 9 end 10 s = s * (1 + r(end)) * 1e4 ˆ Trade-off: efficiency and generality Zheng-Liang Lu 149 / 179

17 Why Numerical Methods? ˆ Very few problems, however, have simple and analytical solutions. ˆ Numerical methods provide computational solutions for many crucial problems, thought the correctness of the numerical methods remains a big issue. 8 ˆ Simulation techniques 9 significantly reduce the cost both in industry and science. ˆ So the simulation techniques generate huge business profit. 8 See Generation_and_propagation_of_errors. 9 See Zheng-Liang Lu 150 / 179

18 All science is dominated by the idea of approximation. Bertrand Russell ( ) Essentially, all models are wrong, but some are useful. George E. P. Box ( ) Zheng-Liang Lu 151 / 179

19 Vectorization (Revisited) 10 ˆ The built-in functions such as sqrt(x) and exp(x) automatically operate on array arguments to produce an array result with the same size as the array argument x. 1 >> t = linspace(0, pi, 5); 2 >> y = sin(t) 3 4 y = More about vectorization. Zheng-Liang Lu 152 / 179

20 Advantages by Vectorization ˆ Appearance: vectorized mathematical code appears more like the mathematical expressions found in textbooks, making the code easier to understand. ˆ Less error prone: without loops, vectorized code is often shorter. ˆ Fewer lines of code mean fewer opportunities to introduce programming errors. ˆ Performance: vectorized code often runs much faster than the corresponding code containing loops. Zheng-Liang Lu 153 / 179

21 Performance Analysis In Real Time ˆ In addition to the theoretical analysis of algorithms, programmers can also use a timer to measure the performance. 11 ˆ Once you identify which functions are consuming the most time, you should determine why you are calling them, then look for alternatives to improve the overall performance. ˆ However, Amdahl s law 12 states that the speedup of a program using multiple processors in parallel computing is limited by the time needed for the sequential fraction of the program. 11 Note that the results may differ depending on the difference of run-time environments, so make sure that you benchmark the algorithms on the same conditions. 12 Amdahl (1967). Zheng-Liang Lu 154 / 179

22 Digress: Amdahl s Law ˆ Assume that a program has some codes which takes α% of total running time and can be further parallelized. ˆ Then the speedup can be achieved by investing p computers. ˆ More explicitly, Speedup = ˆ So we can calculate its limit, lim p α p α p (100 α) = + (100 α) 100 α. ˆ For example, the upper bound of speedup is 2 if α = 50. Zheng-Liang Lu 155 / 179

23 tic & toc ˆ The command tic makes a stopwatch timer start. ˆ The command toc returns the elapsed time ( 10 6 second) from the stopwatch timer started by tic. (Try.) 1 >> tic % Please wait for a second. 2 >> toc 3 4 Elapsed time is seconds. Zheng-Liang Lu 156 / 179

24 Tips for Performance 13 ˆ Preallocate arrays ˆ Repeatedly resizing arrays often requires Matlab to spend extra time looking for larger contiguous blocks of memory, and then moving the array into those blocks. ˆ Vectorize your code ˆ Create new variables if data type changes ˆ Use functions instead of scripts ˆ Avoid overloading Matlab built-in functions 13 See Techniques for Improving Performance. Zheng-Liang Lu 157 / 179

25 Example: A Benchmark 1 clear; clc; 2 3 order = 0 : 1 : 4; 4 t1 = zeros(1, length(order)); 5 t2 = zeros(1, length(order)); 6 t3 = zeros(1, length(order)); 7 8 for j = 1 : length(order) 9 num = 10 ˆ order(j); tic 12 y = []; 13 for i = 1 : num 14 y = [y, i ˆ 2]; % dynamic allocation of... array y 15 end 16 t1(j) = toc; Zheng-Liang Lu 158 / 179

26 17 18 clear y; 19 tic 20 y = zeros(1, num); % preallocation of array y 21 for i = 1 : num 22 y(i) = i ˆ 2; 23 end 24 t2(j) = toc; clear y; 27 t = 1 : 1 : num; 28 tic 29 y = t.ˆ 2; % vectorization 30 t3(j) = toc; 31 end Zheng-Liang Lu 159 / 179

27 t1 / t2 t1 / t3 t2 / t Speedup Array Size Zheng-Liang Lu 160 / 179

28 t1 / t2 t1 / t3 t2 / t3 Speedup (log scale) Array Size (log scale) Zheng-Liang Lu 161 / 179

29 All roads lead to Rome. Anonymous 但如你根本並無招式, 敵人如何來破你的招式? 風清揚 笑傲江湖 第十回 傳劍 Zheng-Liang Lu 162 / 179

30 1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 163 / 179

31 Functions ˆ The first thing of algorithm design is to divide and conquer. ˆ In other words, a large and complicated problem would be conquered by solving its subproblems. ˆ In order to reuse the algorithms without copying the codes, the best way is to make them functions. 14 ˆ The idea of the (software) functions is similar to the math function, which is typically written in form of y = f (x) with the input x and the output y. 14 Make bricks for your castle. Zheng-Liang Lu 164 / 179

32 ˆ A function is a piece of computer code that accepts an input argument from the caller, and returns output argument for the specific job the caller dealing with. ˆ Functions allow you to modularize a program by separating its tasks into self-contained units. ˆ We can program more efficiently and avoid rewriting the computer code for calculations that are performed frequently Remember that the bug propagates when you copy and paste the codes. It is a serious problem especially when you are working on a medium- or huge-level project. Zheng-Liang Lu 165 / 179

33 Arithmetic Functions Zheng-Liang Lu 166 / 179

34 Trigonometric Functions 16 ˆ Recall that 1 rad = 180 π. 16 See Table in Palm, p Zheng-Liang Lu 167 / 179

35 Rounding Functions ˆ Can you organize an algorithm for the function ceil and floor? ˆ How to check if the input is an integer? (Try.) Zheng-Liang Lu 168 / 179

36 Discrete Math Functions Zheng-Liang Lu 169 / 179

37 Max Functions Zheng-Liang Lu 170 / 179

38 ˆ The function min works in a similar way to max. Zheng-Liang Lu 171 / 179

39 Functions for Central Tendency Zheng-Liang Lu 172 / 179

40 Variance and Standard Deviation Zheng-Liang Lu 173 / 179

41 Functions for Sizes Zheng-Liang Lu 174 / 179

42 Random Number Generators ˆ You may use randi(n, m, n) to produce an m-by-n random integer matrix ranging from 1 to N. Zheng-Liang Lu 175 / 179

43 ˆ You can generate a random number sampled from a standard uniform distribution, say by a linear congruential generator. 17 ˆ Be aware that there is no true random number generator in the machines. 18 ˆ Widely used in Monte Carlo simulation 19 and random number generation of other distributions 20. ˆ Use rng( shuffle ) to generate different random sequences. 17 See 18 For now, the modern computers are all deterministic. Quantum computers share theoretical similarities with non-deterministic and probabilistic computers. 19 See Glasserman (2003). 20 See the acceptance-rejection method and Metropolis-Hastings algorithm. Zheng-Liang Lu 176 / 179

44 User-Defined Functions ˆ A user-defined function is created by 1 function [outputvar] = funcname(inputvar) 2 % comment section 3 end ˆ The output variables, if there exist, are enclosed in square brackets. ˆ The input variables, if there exist, must be enclosed with parentheses. ˆ funcname should start with a letter, and be the same as the file name in which it is saved. ˆ Before this function can be used, it must be saved into the current folder If not, change the current folder or add to the path pool. Zheng-Liang Lu 177 / 179

45 Example: Addition of Two Numbers 1 function z = myadd(x, y) 2 % input: x, y (two numbers) 3 % output: z (sum of x and y) 4 z = x + y; 5 end ˆ It seems bloody trivial. ˆ Actually, the plus sign is a kind of syntactic sugar Recall how to use an addition instruction in assembly codes. Zheng-Liang Lu 178 / 179

46 Example: Mean of A Sequence 1 function y = mymean(x) 2 % input: x (array) 3 % output: y (mean) 4 5 sum = 0; 6 n = length(x); 7 for i = 1 : n 8 sum = myadd(sum, x(i)); % call myadd 9 end 10 y = sum / n; 11 end Zheng-Liang Lu 179 / 179

ˆ Note that we often make a trade-off between time and space. ˆ Time complexity ˆ Space complexity. ˆ Unlike time, we can reuse memory.

ˆ Note that we often make a trade-off between time and space. ˆ Time complexity ˆ Space complexity. ˆ Unlike time, we can reuse memory. ˆ We use O-notation to describe the asymptotic 1 upper bound of complexity of the algorithm. ˆ So O-notation is widely used to classify algorithms by how they respond to changes in its input size. 2 ˆ

More information

Jump Statements. ˆ The break statement exits a for or while loop completely.

Jump Statements. ˆ The break statement exits a for or while loop completely. Jump Statements ˆ The break statement exits a for or while loop completely. ˆ No longer in the loop. ˆ Aka early termination. ˆ To skip the rest of the instructions in the loop and begin the next iteration,

More information

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225 1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225 Functions The first thing of the design of algorithms is to divide and conquer. A large and complex problem would be solved by couples

More information

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example, Cloning Arrays In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example, 1... 2 T[] A = {...}; // assume A is an array 3 T[] B = A;

More information

How to swap values of two variables without tmp? However, this naive algorithm is biased. 1

How to swap values of two variables without tmp? However, this naive algorithm is biased. 1 Shuffling over array elements 1... 2 for (int i = 0; i < A.length; ++i) { 3 // choose j randomly 4 int j = (int) (Math.random() A.length); 5 // swap 6 int tmp = A[i]; 7 A[i] = A[j]; 8 A[j] = tmp; 9 } 10...

More information

1 class Lecture5 { 2 3 "Arrays" 4. Zheng-Liang Lu Java Programming 136 / 174

1 class Lecture5 { 2 3 Arrays 4. Zheng-Liang Lu Java Programming 136 / 174 1 class Lecture5 { 2 3 "Arrays" 4 5 } Zheng-Liang Lu Java Programming 136 / 174 Arrays An array stores a large collection of data which is of the same type. 2 // assume the size variable exists above 3

More information

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example, Cloning Arrays In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example, 1... 2 T[] A = {...}; // assume A is an array 3 T[] B = A;

More information

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221 1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221 Functions Recall that an algorithm is a feasible solution to the specific problem. 1 A function is a piece of computer code that accepts

More information

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls. Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:

More information

Nested Loops. A loop can be nested inside another loop.

Nested Loops. A loop can be nested inside another loop. Nested Loops A loop can be nested inside another loop. Nested loops consist of an outer loop and one or more inner loops. Each time the outer loop is repeated, the inner loops are reentered, and started

More information

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls. Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:

More information

1 class Lecture6 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 10 / Zheng-Liang Lu Java Programming 185 / 248

1 class Lecture6 { 2 3 Methods / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 10 / Zheng-Liang Lu Java Programming 185 / 248 1 class Lecture6 { 2 3 "Methods" 4 5 } 6 7 / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 10 / Zheng-Liang Lu Java Programming 185 / 248 All roads lead to Rome. Anonymous 但如你根本並無招式, 敵人如何來破你的招式? 風清揚,

More information

1 class Lecture6 { 2 3 "Methods" // keywords: 8 return. Zheng-Liang Lu Java Programming 186 / 244

1 class Lecture6 { 2 3 Methods // keywords: 8 return. Zheng-Liang Lu Java Programming 186 / 244 1 class Lecture6 { 2 3 "Methods" 4 5 } 6 7 // keywords: 8 return Zheng-Liang Lu Java Programming 186 / 244 Methods 2 Methods can be used to define reusable code, and organize and simplify code. The idea

More information

Exercise (Revisited)

Exercise (Revisited) Exercise (Revisited) Redo the cashier problem by using an infinite loop with a break statement. 1... 2 while (true) { 3 System.out.println("Enter price?"); 4 price = input.nextint(); 5 if (price

More information

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017 8/3/07 Analysis Introduction to Analysis Model of Analysis Mathematical Preliminaries for Analysis Set Notation Asymptotic Analysis What is an algorithm? An algorithm is any well-defined computational

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

More information

Variables and Assignments

Variables and Assignments Variables and Assignments ˆ A variable is used to keep a value or values. ˆ A box which contains something. ˆ In most languages, a statement looks like var = expression, where var is a variable and expression

More information

LECTURE 0: Introduction and Background

LECTURE 0: Introduction and Background 1 LECTURE 0: Introduction and Background September 10, 2012 1 Computational science The role of computational science has become increasingly significant during the last few decades. It has become the

More information

Introduction to Algorithms 6.046J/18.401J

Introduction to Algorithms 6.046J/18.401J Introduction to Algorithms 6.046J/18.401J LECTURE 1 Analysis of Algorithms Insertion sort Merge sort Prof. Charles E. Leiserson Course information 1. Staff. Prerequisites 3. Lectures 4. Recitations 5.

More information

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005 CS302 Topic: Algorithm Analysis Thursday, Sept. 22, 2005 Announcements Lab 3 (Stock Charts with graphical objects) is due this Friday, Sept. 23!! Lab 4 now available (Stock Reports); due Friday, Oct. 7

More information

Selections. Zheng-Liang Lu 91 / 120

Selections. Zheng-Liang Lu 91 / 120 Selections ˆ Selection enables us to write programs that make decisions on. ˆ Selection structures contain one or more of the if, else, and elseif statements. ˆ The end statement denotes the end of selection

More information

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48 Algorithm Analysis (Algorithm Analysis ) Data Structures and Programming Spring 2018 1 / 48 What is an Algorithm? An algorithm is a clearly specified set of instructions to be followed to solve a problem

More information

Introduction to Algorithms 6.046J/18.401J/SMA5503

Introduction to Algorithms 6.046J/18.401J/SMA5503 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 1 Prof. Charles E. Leiserson Welcome to Introduction to Algorithms, Fall 01 Handouts 1. Course Information. Calendar 3. Registration (MIT students

More information

Lecture Notes for Chapter 2: Getting Started

Lecture Notes for Chapter 2: Getting Started Instant download and all chapters Instructor's Manual Introduction To Algorithms 2nd Edition Thomas H. Cormen, Clara Lee, Erica Lin https://testbankdata.com/download/instructors-manual-introduction-algorithms-2ndedition-thomas-h-cormen-clara-lee-erica-lin/

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

More information

CS240 Fall Mike Lam, Professor. Algorithm Analysis

CS240 Fall Mike Lam, Professor. Algorithm Analysis CS240 Fall 2014 Mike Lam, Professor Algorithm Analysis Algorithm Analysis Motivation: what and why Mathematical functions Comparative & asymptotic analysis Big-O notation ("Big-Oh" in textbook) Analyzing

More information

The return Statement

The return Statement The return Statement The return statement is the end point of the method. A callee is a method invoked by a caller. The callee returns to the caller if the callee completes all the statements (w/o a return

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 45

More information

Arithmetic Compound Assignment Operators

Arithmetic Compound Assignment Operators Arithmetic Compound Assignment Operators Note that these shorthand operators are not available in languages such as Matlab and R. Zheng-Liang Lu Java Programming 76 / 172 Example 1... 2 int x = 1; 3 System.out.println(x);

More information

Outline and Reading. Analysis of Algorithms 1

Outline and Reading. Analysis of Algorithms 1 Outline and Reading Algorithms Running time ( 3.1) Pseudo-code ( 3.2) Counting primitive operations ( 3.4) Asymptotic notation ( 3.4.1) Asymptotic analysis ( 3.4.2) Case study ( 3.4.3) Analysis of Algorithms

More information

Theory and Algorithms Introduction: insertion sort, merge sort

Theory and Algorithms Introduction: insertion sort, merge sort Theory and Algorithms Introduction: insertion sort, merge sort Rafael Ramirez rafael@iua.upf.es Analysis of algorithms The theoretical study of computer-program performance and resource usage. What s also

More information

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment Class: V - CE Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology Sub: Design and Analysis of Algorithms Analysis of Algorithm: Assignment

More information

Chapter 2: Complexity Analysis

Chapter 2: Complexity Analysis Chapter 2: Complexity Analysis Objectives Looking ahead in this chapter, we ll consider: Computational and Asymptotic Complexity Big-O Notation Properties of the Big-O Notation Ω and Θ Notations Possible

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis Computer Science 210 Data Structures Siena College Fall 2017 Topic Notes: Complexity and Asymptotic Analysis Consider the abstract data type, the Vector or ArrayList. This structure affords us the opportunity

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

More information

MBI REU Matlab Tutorial

MBI REU Matlab Tutorial MBI REU Matlab Tutorial Lecturer: Reginald L. McGee II, Ph.D. June 8, 2017 MATLAB MATrix LABoratory MATLAB is a tool for numerical computation and visualization which allows Real & Complex Arithmetics

More information

CSC630/CSC730 Parallel & Distributed Computing

CSC630/CSC730 Parallel & Distributed Computing CSC630/CSC730 Parallel & Distributed Computing Analytical Modeling of Parallel Programs Chapter 5 1 Contents Sources of Parallel Overhead Performance Metrics Granularity and Data Mapping Scalability 2

More information

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES DESIGN AND ANALYSIS OF ALGORITHMS Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES http://milanvachhani.blogspot.in USE OF LOOPS As we break down algorithm into sub-algorithms, sooner or later we shall

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Spring 2019 Alexis Maciel Department of Computer Science Clarkson University Copyright c 2019 Alexis Maciel ii Contents 1 Analysis of Algorithms 1 1.1 Introduction.................................

More information

Introduction to Matlab Programming with Applications

Introduction to Matlab Programming with Applications Introduction to Matlab Programming with Applications Zheng-Liang Lu Department of Computer Science and Information Engineering National Taiwan University Matlab 256 Summer 2015 Class Information Official

More information

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES DESIGN AND ANALYSIS OF ALGORITHMS Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES http://milanvachhani.blogspot.in USE OF LOOPS As we break down algorithm into sub-algorithms, sooner or later we shall

More information

Arrays. ˆ An array, is a linear data structure consisting of a collection of elements, each identified by one array index. ˆ For math, arrays could be

Arrays. ˆ An array, is a linear data structure consisting of a collection of elements, each identified by one array index. ˆ For math, arrays could be Arrays ˆ An array, is a linear data structure consisting of a collection of elements, each identified by one array index. ˆ For math, arrays could be ˆ row vectors: u R 1 n for any positive integer n ˆ

More information

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226 Method Invocation Note that the input parameters are sort of variables declared within the method as placeholders. When calling the method, one needs to provide arguments, which must match the parameters

More information

Chapter 1. Introduction

Chapter 1. Introduction Chapter 1 Introduction A Monte Carlo method is a compuational method that uses random numbers to compute (estimate) some quantity of interest. Very often the quantity we want to compute is the mean of

More information

switch-case Statements

switch-case Statements switch-case Statements A switch-case structure takes actions depending on the target variable. 2 switch (target) { 3 case v1: 4 // statements 5 break; 6 case v2: 7. 8. 9 case vk: 10 // statements 11 break;

More information

Arithmetic Compound Assignment Operators

Arithmetic Compound Assignment Operators Arithmetic Compound Assignment Operators Note that these shorthand operators are not available in languages such as Matlab and R. Zheng-Liang Lu Java Programming 76 / 141 Example 1... 2 int x = 1; 3 System.out.println(x);

More information

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

1 class Lecture5 { 2 3 Methods / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199 1 class Lecture5 { 2 3 "Methods" 4 5 } 6 7 / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199 Methods 2 Methods can be used to define reusable code, and organize

More information

Why study algorithms? CS 561, Lecture 1. Today s Outline. Why study algorithms? (II)

Why study algorithms? CS 561, Lecture 1. Today s Outline. Why study algorithms? (II) Why study algorithms? CS 561, Lecture 1 Jared Saia University of New Mexico Seven years of College down the toilet - John Belushi in Animal House Q: Can I get a programming job without knowing something

More information

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms DATA STRUCTURES AND ALGORITHMS Asymptotic analysis of the algorithms Summary of the previous lecture Algorithm definition Representation of the algorithms: Flowchart, Pseudocode Description Types of the

More information

Introduction to Matlab Programming with Applications

Introduction to Matlab Programming with Applications Introduction to Matlab Programming with Applications Zheng-Liang Lu Department of Computer Science and Information Engineering National Taiwan University Matlab 289 Summer 2017 Class Information ˆ The

More information

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87 Data Types Java is a strongly-typed 1 programming language. Every variable has a type. Also, every (mathematical) expression has a type. There are two categories of data types: primitive data types, and

More information

CS4311 Design and Analysis of Algorithms. Lecture 1: Getting Started

CS4311 Design and Analysis of Algorithms. Lecture 1: Getting Started CS4311 Design and Analysis of Algorithms Lecture 1: Getting Started 1 Study a few simple algorithms for sorting Insertion Sort Selection Sort Merge Sort About this lecture Show why these algorithms are

More information

Vectorization: An Introduction. Dr. Marco A. Arocha INGE3016-MATLAB Summer 2015

Vectorization: An Introduction. Dr. Marco A. Arocha INGE3016-MATLAB Summer 2015 Vectorization: An Introduction Dr. Marco A. Arocha INGE3016-MATLAB Summer 2015 1 Array and Matrix Operations Operation Operator Explanation Array addition a + b array addition and matrix addition are identical

More information

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 2 - Jan. 9, 2019 CLRS 1.1, 1.2, 2.2, 3.1, 4.3, 4.5 University of Manitoba Picture is from the cover of the textbook CLRS. 1 /

More information

Introduction to Engineering gii

Introduction to Engineering gii 25.108 Introduction to Engineering gii Dr. Jay Weitzen Lecture Notes I: Introduction to Matlab from Gilat Book MATLAB - Lecture # 1 Starting with MATLAB / Chapter 1 Topics Covered: 1. Introduction. 2.

More information

PART 1 PROGRAMMING WITH MATHLAB

PART 1 PROGRAMMING WITH MATHLAB PART 1 PROGRAMMING WITH MATHLAB Presenter: Dr. Zalilah Sharer 2018 School of Chemical and Energy Engineering Universiti Teknologi Malaysia 23 September 2018 Programming with MATHLAB MATLAB Environment

More information

Pseudo code of algorithms are to be read by.

Pseudo code of algorithms are to be read by. Cs502 Quiz No1 Complete Solved File Pseudo code of algorithms are to be read by. People RAM Computer Compiler Approach of solving geometric problems by sweeping a line across the plane is called sweep.

More information

Analytical Modeling of Parallel Programs

Analytical Modeling of Parallel Programs Analytical Modeling of Parallel Programs Alexandre David Introduction to Parallel Computing 1 Topic overview Sources of overhead in parallel programs. Performance metrics for parallel systems. Effect of

More information

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS Lecture 03-04 PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS By: Dr. Zahoor Jan 1 ALGORITHM DEFINITION A finite set of statements that guarantees an optimal solution in finite interval of time 2 GOOD ALGORITHMS?

More information

(Refer Slide Time: 1:27)

(Refer Slide Time: 1:27) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 1 Introduction to Data Structures and Algorithms Welcome to data

More information

Variable Scope. The variable scope is the range of the program where the variable can be referenced.

Variable Scope. The variable scope is the range of the program where the variable can be referenced. Variable Scope The variable scope is the range of the program where the variable can be referenced. Variables can be declared in class level, method level, and loop level. In general, a pair of curly brackets

More information

Scope of Variables. In general, it is not a good practice to define many global variables. 1. Use global to declare x as a global variable.

Scope of Variables. In general, it is not a good practice to define many global variables. 1. Use global to declare x as a global variable. Scope of Variables The variables used in function m-files are known as local variables. Any variable defined within the function exists only for the function to use. The only way a function can communicate

More information

Analysis of Algorithms Part I: Analyzing a pseudo-code

Analysis of Algorithms Part I: Analyzing a pseudo-code Analysis of Algorithms Part I: Analyzing a pseudo-code Introduction Pseudo-code representation of an algorithm Analyzing algorithms Measuring the running time and memory size of an algorithm Calculating

More information

Introduction to Algorithms 6.046J/18.401J

Introduction to Algorithms 6.046J/18.401J Introduction to Algorithms 6.046J/18.401J Lecture 1 Prof. Piotr Indyk Analysis of algorithms The theoretical study of computer-program performance and resource usage. Also important: modularity maintainability

More information

Merge Sort. 25. Efficiency. Insertion sort vs. merge sort Timing with tic toc Time efficiency vs. memory efficiency

Merge Sort. 25. Efficiency. Insertion sort vs. merge sort Timing with tic toc Time efficiency vs. memory efficiency 25. Efficiency Insertion sort vs. merge sort Timing with tic toc Time efficiency vs. memory efficiency Announcements: - P6 will be posted today, due 11/29 - Final exam conflict? Email Kelly Patwell with

More information

Design of Parallel Algorithms. Course Introduction

Design of Parallel Algorithms. Course Introduction + Design of Parallel Algorithms Course Introduction + CSE 4163/6163 Parallel Algorithm Analysis & Design! Course Web Site: http://www.cse.msstate.edu/~luke/courses/fl17/cse4163! Instructor: Ed Luke! Office:

More information

IEEE Floating-Point Representation 1

IEEE Floating-Point Representation 1 IEEE Floating-Point Representation 1 x = ( 1) s M 2 E The sign s determines whether the number is negative (s = 1) or positive (s = 0). The significand M is a fractional binary number that ranges either

More information

Floating Point Arithmetic

Floating Point Arithmetic Floating Point Arithmetic CS 365 Floating-Point What can be represented in N bits? Unsigned 0 to 2 N 2s Complement -2 N-1 to 2 N-1-1 But, what about? very large numbers? 9,349,398,989,787,762,244,859,087,678

More information

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs Algorithms in Systems Engineering IE172 Midterm Review Dr. Ted Ralphs IE172 Midterm Review 1 Textbook Sections Covered on Midterm Chapters 1-5 IE172 Review: Algorithms and Programming 2 Introduction to

More information

Introduction to Matlab

Introduction to Matlab Introduction to Matlab By:Mohammad Sadeghi *Dr. Sajid Gul Khawaja Slides has been used partially to prepare this presentation Outline: What is Matlab? Matlab Screen Basic functions Variables, matrix, indexing

More information

Logic is the anatomy of thought. John Locke ( ) This sentence is false.

Logic is the anatomy of thought. John Locke ( ) This sentence is false. Logic is the anatomy of thought. John Locke (1632 1704) This sentence is false. I know that I know nothing. anonymous Plato (In Apology, Plato relates that Socrates accounts for his seeming wiser than

More information

IBM 370 Basic Data Types

IBM 370 Basic Data Types IBM 370 Basic Data Types This lecture discusses the basic data types used on the IBM 370, 1. Two s complement binary numbers 2. EBCDIC (Extended Binary Coded Decimal Interchange Code) 3. Zoned Decimal

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College August 31, 2015 Outline Outline 1 Chapter 1 Outline Textbook Data Structures and Algorithms Using Python and C++ David M.

More information

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms Algorithm Efficiency & Sorting Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms Overview Writing programs to solve problem consists of a large number of decisions how to represent

More information

Dr Richard Greenaway

Dr Richard Greenaway SCHOOL OF PHYSICS, ASTRONOMY & MATHEMATICS 4PAM1008 MATLAB 2 Basic MATLAB Operation Dr Richard Greenaway 2 Basic MATLAB Operation 2.1 Overview 2.1.1 The Command Line In this Workshop you will learn how

More information

Example: Monte Carlo Simulation 1

Example: Monte Carlo Simulation 1 Example: Monte Carlo Simulation 1 Write a program which conducts a Monte Carlo simulation to estimate π. 1 See https://en.wikipedia.org/wiki/monte_carlo_method. Zheng-Liang Lu Java Programming 133 / 149

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College September 6, 2017 Outline Outline 1 Chapter 2: Data Abstraction Outline Chapter 2: Data Abstraction 1 Chapter 2: Data Abstraction

More information

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS LECTURE 1 Babeş - Bolyai University Computer Science and Mathematics Faculty 2017-2018 Overview Course organization 1 Course organization 2 3 4 Course Organization I Guiding teachers Lecturer PhD. Marian

More information

Program Calculus Calculational Programming

Program Calculus Calculational Programming Program Calculus Calculational Programming National Institute of Informatics June 21 / June 28 / July 5, 2010 Program Calculus Calculational Programming What we will learn? Discussing the mathematical

More information

Monte Carlo Integration and Random Numbers

Monte Carlo Integration and Random Numbers Monte Carlo Integration and Random Numbers Higher dimensional integration u Simpson rule with M evaluations in u one dimension the error is order M -4! u d dimensions the error is order M -4/d u In general

More information

Introduction to Modeling. Lecture Overview

Introduction to Modeling. Lecture Overview Lecture Overview What is a Model? Uses of Modeling The Modeling Process Pose the Question Define the Abstractions Create the Model Analyze the Data Model Representations * Queuing Models * Petri Nets *

More information

Ph3 Mathematica Homework: Week 1

Ph3 Mathematica Homework: Week 1 Ph3 Mathematica Homework: Week 1 Eric D. Black California Institute of Technology v1.1 1 Obtaining, installing, and starting Mathematica Exercise 1: If you don t already have Mathematica, download it and

More information

HOW TO WRITE PARALLEL PROGRAMS AND UTILIZE CLUSTERS EFFICIENTLY

HOW TO WRITE PARALLEL PROGRAMS AND UTILIZE CLUSTERS EFFICIENTLY HOW TO WRITE PARALLEL PROGRAMS AND UTILIZE CLUSTERS EFFICIENTLY Sarvani Chadalapaka HPC Administrator University of California Merced, Office of Information Technology schadalapaka@ucmerced.edu it.ucmerced.edu

More information

LAB: INTRODUCTION TO FUNCTIONS IN C++

LAB: INTRODUCTION TO FUNCTIONS IN C++ LAB: INTRODUCTION TO FUNCTIONS IN C++ MODULE 2 JEFFREY A. STONE and TRICIA K. CLARK COPYRIGHT 2014 VERSION 4.0 PALMS MODULE 2 LAB: FUNCTIONS IN C++ 2 Introduction This lab will provide students with an

More information

Introduction to Data Structure

Introduction to Data Structure Introduction to Data Structure CONTENTS 1.1 Basic Terminology 1. Elementary data structure organization 2. Classification of data structure 1.2 Operations on data structures 1.3 Different Approaches to

More information

CS240 Fall Mike Lam, Professor. Algorithm Analysis

CS240 Fall Mike Lam, Professor. Algorithm Analysis CS240 Fall 2014 Mike Lam, Professor Algorithm Analysis HW1 Grades are Posted Grades were generally good Check my comments! Come talk to me if you have any questions PA1 is Due 9/17 @ noon Web-CAT submission

More information

Class Note #02. [Overall Information] [During the Lecture]

Class Note #02. [Overall Information] [During the Lecture] Class Note #02 Date: 01/11/2006 [Overall Information] In this class, after a few additional announcements, we study the worst-case running time of Insertion Sort. The asymptotic notation (also called,

More information

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1 CS241 - week 1 review Special classes of algorithms: logarithmic: O(log n) linear: O(n) quadratic: O(n 2 ) polynomial: O(n k ), k 1 exponential: O(a n ), a > 1 Classifying algorithms is generally done

More information

Data Structures and Algorithms CSE 465

Data Structures and Algorithms CSE 465 Data Structures and Algorithms CSE 465 LECTURE 2 Analysis of Algorithms Insertion Sort Loop invariants Asymptotic analysis Sofya Raskhodnikova and Adam Smith The problem of sorting Input: sequence a 1,

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithm Analysis Page 1 - Algorithm Analysis Dr. Fall 2008 Algorithm Analysis Page 2 Outline Textbook Overview Analysis of Algorithm Pseudo-Code and Primitive Operations Growth Rate and Big-Oh Notation

More information

Outline: Embarrassingly Parallel Problems. Example#1: Computation of the Mandelbrot Set. Embarrassingly Parallel Problems. The Mandelbrot Set

Outline: Embarrassingly Parallel Problems. Example#1: Computation of the Mandelbrot Set. Embarrassingly Parallel Problems. The Mandelbrot Set Outline: Embarrassingly Parallel Problems Example#1: Computation of the Mandelbrot Set what they are Mandelbrot Set computation cost considerations static parallelization dynamic parallelizations and its

More information

Lecture 5: Running Time Evaluation

Lecture 5: Running Time Evaluation Lecture 5: Running Time Evaluation Worst-case and average-case performance Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 13 1 Time complexity 2 Time growth 3 Worst-case 4 Average-case

More information

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes. LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes. Input Algorithm Output An algorithm is a step-by-step procedure for

More information

Data Structures Lecture 8

Data Structures Lecture 8 Fall 2017 Fang Yu Software Security Lab. Dept. Management Information Systems, National Chengchi University Data Structures Lecture 8 Recap What should you have learned? Basic java programming skills Object-oriented

More information

CS301 - Data Structures Glossary By

CS301 - Data Structures Glossary By CS301 - Data Structures Glossary By Abstract Data Type : A set of data values and associated operations that are precisely specified independent of any particular implementation. Also known as ADT Algorithm

More information

2.3 Algorithms Using Map-Reduce

2.3 Algorithms Using Map-Reduce 28 CHAPTER 2. MAP-REDUCE AND THE NEW SOFTWARE STACK one becomes available. The Master must also inform each Reduce task that the location of its input from that Map task has changed. Dealing with a failure

More information

Global Variables. ˆ Unlike local variables, global variables are available to all functions involved.

Global Variables. ˆ Unlike local variables, global variables are available to all functions involved. Global Variables ˆ Unlike local variables, global variables are available to all functions involved. ˆ Use global to declare x as global. ˆ For example, the universal constant, say,. 1 ˆ However, it is

More information

Microsoft Access 2016

Microsoft Access 2016 Access 2016 Instructor s Manual Page 1 of 10 Microsoft Access 2016 Module Two: Querying a Database A Guide to this Instructor s Manual: We have designed this Instructor s Manual to supplement and enhance

More information

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong Department of Computer Science and Engineering Chinese University of Hong Kong In this lecture, we will design the merge sort which sorts n elements in O(n log n) time. The algorithm illustrates a technique

More information

Microsoft Access 2016

Microsoft Access 2016 Access 2016 Instructor s Manual Page 1 of 10 Microsoft Access 2016 Module Two: Querying a Database A Guide to this Instructor s Manual: We have designed this Instructor s Manual to supplement and enhance

More information